package com.zenithmind.library.strategy;

import com.zenithmind.library.enums.BookStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 图书状态策略工厂
 * 遵循开闭原则：通过工厂模式管理策略，便于扩展新的状态处理策略
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Component
@RequiredArgsConstructor
public class BookStatusStrategyFactory {

    private final List<BookStatusStrategy> strategies;
    private Map<BookStatus, BookStatusStrategy> strategyMap;

    /**
     * 初始化策略映射
     */
    private void initStrategyMap() {
        if (strategyMap == null) {
            strategyMap = strategies.stream()
                    .collect(Collectors.toMap(
                            BookStatusStrategy::getSupportedStatus,
                            Function.identity()
                    ));
        }
    }

    /**
     * 获取指定状态的处理策略
     * 
     * @param status 图书状态
     * @return 处理策略
     */
    public BookStatusStrategy getStrategy(BookStatus status) {
        initStrategyMap();
        return strategyMap.get(status);
    }

    /**
     * 检查是否支持指定状态
     * 
     * @param status 图书状态
     * @return 是否支持
     */
    public boolean isSupported(BookStatus status) {
        initStrategyMap();
        return strategyMap.containsKey(status);
    }

    /**
     * 获取所有支持的状态
     * 
     * @return 支持的状态列表
     */
    public List<BookStatus> getSupportedStatuses() {
        initStrategyMap();
        return List.copyOf(strategyMap.keySet());
    }
}
